package org.dizena.modules.master.service;

import org.dizena.base.avo.req.JqGridReq;
import org.dizena.base.avo.resp.JqGridResp;
import org.dizena.modules.master.bean.Res;
import org.dizena.modules.master.bean.Role;
import org.dizena.modules.master.dao.RessDao;
import org.dizena.modules.master.dao.RoleDao;
import org.dizena.utils.IdGen;
import org.dizena.utils.JqGridRespBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RoleService
{

    @Resource
    private RoleDao roleDao;
    @Resource
    private RessDao ressDao;
    @Resource
    private JqGridRespBuilder<Role> jqGridRespBuilder;

    public JqGridResp<Role> list(JqGridReq req)
    {
        return jqGridRespBuilder.build(req, roleDao);
    }

    public String crud(String oper, Role role)
    {
        if ("add".equals(oper))
        {
            Role tmp = roleDao.getByRole(role.getRole());
            if (tmp != null)
            {
                return "role exist";
            }
            role.setId(IdGen.get().nextIdS());
            roleDao.add(role);
        }
        else if ("edit".equals(oper))
        {
            Role tmp = roleDao.getByRole(role.getRole());
            if (tmp != null)
            {
                return "role exist";
            }
            roleDao.update(role);
        }
        else if ("del".equals(oper))
        {
            String ids = role.getId();
            for (String s : ids.split(","))
            {
                roleDao.deleteById(s);
            }
        }
        return "ok";
    }

    public Role get(String id)
    {

        return roleDao.get(id);
    }

    public Map<String, Object> getRoleRess(String id)
    {
        Role role = roleDao.get(id);
        List<String> ids = role.getRessIds();
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("id", "0");
        json.put("text", "所有菜单");
        if (ids != null && ids.size() > 0)
        {
            if (ids.contains("0"))
            {
                Map<String, Object> tmp = new HashMap<>();
                tmp.put("selected", true);
                json.put("state", tmp);
                ids.remove("0");
            }
        }
        List<Map<String, Object>> nodes = getMenuTreeData("0", 1, ids);
        json.put("nodes", nodes);
        return json;

    }

    public Collection<Res> queryRoleRess(String role)
    {
        List<Res> resss = new LinkedList<Res>();
        Role r = roleDao.getByRole(role);
        List<String> lists = r.getRessIds();
        if (lists != null && lists.size() > 0)
        {
            for (String rid : lists)
            {
                Res res = ressDao.get(rid);
                if (null != res)
                {
                    resss.add(res);
                }
            }
        }
        return resss;
    }

    private List<Map<String, Object>> getMenuTreeData(String pid, Integer level, List<String> ids)
    {
        List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
        List<Res> ress = queryResByLP(pid, level);
        for (Res r : ress)
        {
            if (r != null)
            {
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("id", r.getId());
                m.put("text", r.getResName());
                if (ids != null && ids.size() > 0)
                {
                    if (ids.contains(r.getId()))
                    {
                        Map<String, Object> tmp = new HashMap<>();
                        tmp.put("selected", true);
                        m.put("state", tmp);
                        ids.remove(r.getId());
                    }
                }
                List<Map<String, Object>> tmpLists = getMenuTreeData(r.getId(), r.getResLevel() + 1, ids);
                if (tmpLists.size() > 0)
                {
                    m.put("nodes", tmpLists);
                }
                lists.add(m);
            }
        }
        return lists;
    }

    private List<Res> queryResByLP(String resPid, Integer resLevel)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("resPid", resPid);
        map.put("resLevel", resLevel);
        JqGridReq req = new JqGridReq();
        req.setSord("ASC");
        req.setSidx("resSort");
        return ressDao.queryAll(req, map);
    }


}
